package com.meihua.baoleyi.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.knowledgeBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.ui.CommonWebActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liming on 2017/10/18.
 * 百科知识适配器
 */

public class KnowledgeAdapter extends RecyclerView.Adapter<KnowledgeAdapter.ViewHolder> {

    private List<knowledgeBean> list;
    private Context mContext;

    public KnowledgeAdapter(Context mContext, List<knowledgeBean> list) {
        this.list = list;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_knowledge_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ((ViewHolder)holder).displayData(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size() > 0 ? list.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_title)
        TextView  mTv_title;
        @Bind(R.id.iv_picture)
        ImageView mIv_picture;
        @Bind(R.id.tv_context)
        TextView  mTv_context;
        @Bind(R.id.ll_item)
        LinearLayout  mLl_item;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this,view);
        }

        public void displayData(final knowledgeBean data) {
            mTv_title.setText(data.getBrief_title());
            Glide.with(mContext).load(data.getCover_img()).into(mIv_picture);

            //点击进入百科详情
            mLl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mContext != null) {
//                        mContext.startActivity(BaikeDetailsActivity.createIntent(mContext, data.getId()));
                        Intent intent = new Intent(mContext, CommonWebActivity.class);
                        intent.putExtra("url", APIServer.KNOWDETAIL+data.getId()+"/"+ MyApp.getInstance().getToken());
                        mContext.startActivity(intent);
                    }
                }
            });
        }
    }
}

